#coding:utf-8

import random
from .settings import USER_AGENT_LIST


class RandomUserAgentMiddleware(object):
    """下载中间件, 在Downloader处理请求前,采用随机User-Agent"""
    def process_request(self, request, spider):
        #获取一个新的User-Agent
        user_agent = random.choice(USER_AGENT_LIST)

        #更改当前请求的User-Agent
        request.headers["User-Agent"] = user_agent

        print("----"*30)
        print(request.headers)

        #这里不能return request,因为在Downloader中只有在请求失败时才会将request返回给引擎
        # return request


class RandomProxyMiddleware(object):
    """下载中间件,添加用户代理"""
    def process_request(self, request, spider):

        # 代理地址: http://kps.kdlapi.com/api/getkps/?orderid=914194268627142&num=1&pt=1&sep=1
        #验证代理,需要提供账户名密码和ip:port
        proxy = "http://maozhaojun:ntkn0npx@47.95.197.50:16818"
        #免费代理
        # proxy = "http://39.106.10.232:16818"

        # 给当前请求添加代理信息
        request.meta['proxy'] = proxy